package com.raceway.ihcenterapp.web;

import com.raceway.ihcenterapp.domain.HelpRequest;
import com.raceway.ihcenterapp.domain.IHS;
import com.raceway.ihcenterapp.domain.Person;
import com.raceway.ihcenterapp.domain.PhoneInfo;
import com.raceway.ihcenterapp.domain.UserRole;

import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.WebUtils;

@RooWebScaffold(path = "phoneinfoes", formBackingObject = PhoneInfo.class,update=false,delete=false,create=false)
@RequestMapping("/phoneinfoes")
@Controller
public class PhoneInfoController {

	@RequestMapping(method = RequestMethod.POST)
    public String create(@Valid PhoneInfo phoneInfo, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("phoneInfo", phoneInfo);
            return "phoneinfoes/create";
        }
        phoneInfo.persist();
        return "redirect:/phoneinfoes/" + encodeUrlPathSegment(phoneInfo.getId().toString(), request);
    }

	@RequestMapping(params = "form", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("phoneInfo", new PhoneInfo());
        return "phoneinfoes/create";
    }

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") Long id, Model model) {
        model.addAttribute("phoneinfo", PhoneInfo.findPhoneInfo(id));
        model.addAttribute("itemId", id);
        return "phoneinfoes/show";
    }

	@RequestMapping(method = RequestMethod.GET)
    public String list(@RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model model) {

		if (page != null || size != null) {
            int sizeNo = size == null ? 10 : size.intValue();
            model.addAttribute("phoneinfoes", PhoneInfo.findPhoneInfoEntries(page == null ? 0 : (page.intValue() - 1) * sizeNo, sizeNo));
            float nrOfPages = (float) PhoneInfo.countPhoneInfoes() / sizeNo;
            model.addAttribute("maxPages", (int) ((nrOfPages > (int) nrOfPages || nrOfPages == 0.0) ? nrOfPages + 1 : nrOfPages));
        } else {
            model.addAttribute("phoneinfoes", PhoneInfo.findAllPhoneInfoes());
        }
		
        return "phoneinfoes/list";
    }

	@RequestMapping(method = RequestMethod.PUT)
    public String update(@Valid PhoneInfo phoneInfo, BindingResult result, Model model, HttpServletRequest request) {
        if (result.hasErrors()) {
            model.addAttribute("phoneInfo", phoneInfo);
            return "phoneinfoes/update";
        }
        phoneInfo.merge();
        return "redirect:/phoneinfoes/" + encodeUrlPathSegment(phoneInfo.getId().toString(), request);
    }

	@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("phoneInfo", PhoneInfo.findPhoneInfo(id));
        return "phoneinfoes/update";
    }

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") Long id, @RequestParam(value = "page", required = false) Integer page, @RequestParam(value = "size", required = false) Integer size, Model model) {
        PhoneInfo.findPhoneInfo(id).remove();
        model.addAttribute("page", (page == null) ? "1" : page.toString());
        model.addAttribute("size", (size == null) ? "10" : size.toString());
        return "redirect:/phoneinfoes?page=" + ((page == null) ? "1" : page.toString()) + "&size=" + ((size == null) ? "10" : size.toString());
    }

	@RequestMapping(params = { "find=ByAndroidId", "form" }, method = RequestMethod.GET)
    public String findPhoneInfoesByAndroidIdForm(Model model) {
        return "phoneinfoes/findPhoneInfoesByAndroidId";
    }

	@RequestMapping(params = "find=ByAndroidId", method = RequestMethod.GET)
    public String findPhoneInfoesByAndroidId(@RequestParam("androidId") String androidId, Model model) {
        model.addAttribute("phoneinfoes", PhoneInfo.findPhoneInfoesByAndroidId(androidId).getResultList());
        return "phoneinfoes/list";
    }

	@RequestMapping(params = { "find=ByAndroidIdEquals", "form" }, method = RequestMethod.GET)
    public String findPhoneInfoesByAndroidIdEqualsForm(Model model) {
        return "phoneinfoes/findPhoneInfoesByAndroidIdEquals";
    }

	@RequestMapping(params = "find=ByAndroidIdEquals", method = RequestMethod.GET)
    public String findPhoneInfoesByAndroidIdEquals(@RequestParam("androidId") String androidId, Model model) {
        model.addAttribute("phoneinfoes", PhoneInfo.findPhoneInfoesByAndroidIdEquals(androidId).getResultList());
        return "phoneinfoes/list";
    }

	String encodeUrlPathSegment(String pathSegment, HttpServletRequest request) {
        String enc = request.getCharacterEncoding();
        if (enc == null) {
            enc = WebUtils.DEFAULT_CHARACTER_ENCODING;
        }
        try {
            pathSegment = UriUtils.encodePathSegment(pathSegment, enc);
        }
        catch (UnsupportedEncodingException uee) {}
        return pathSegment;
    }
}
